package com.example.enterprise

import android.os.Bundle
import android.view.View
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.example.common.view.view.BaseMvvmActivity
import com.example.enterprise.adapter.CheckApproveAdapter
import com.example.enterprise.databinding.ActivityCheckApproveBinding
import com.example.enterprise.vm.MerchantViewModel
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route (path = "/enterprise/CheckApproveActivity")
class CheckApproveActivity : BaseMvvmActivity<ActivityCheckApproveBinding>() {
    override fun getLayoutId(): Int =R.layout.activity_check_approve
    var name:String="新建"
    val checkApproveAdapter=CheckApproveAdapter()
    val merchantViewModel:MerchantViewModel by viewModels()
    override fun initView() {
        super.initView()
        //返回
        mDataBinding.ivBack.setOnClickListener {
            finish()
        }
        mDataBinding.rg.setOnCheckedChangeListener { group, checkedId ->
            when(checkedId){
                R.id.rb1->{
                    mDataBinding.rb1.isChecked=true
                    mDataBinding.rb2.isChecked=false
                    name="新建"
                }
                R.id.rb2->{
                    mDataBinding.rb1.isChecked=false
                    mDataBinding.rb2.isChecked=true
                    name="已审核"
                }
            }
            if (mDataBinding.rb1.isChecked){
                name="新建"
            }else{
                name="已审核"
            }
            merchantViewModel.getMerchant()
        }
        merchantViewModel.getMerchant()
        //发数据
        mDataBinding.rv.adapter=checkApproveAdapter
        mDataBinding.rv.layoutManager=LinearLayoutManager(this)
        merchantViewModel.mSuccessLiveData.observe(this){
            checkApproveAdapter.data.clear()
            if (name .equals( "新建")) {
                checkApproveAdapter.data.addAll(it.data.filter { it.status =="新建" })
            }else{
                checkApproveAdapter.data.addAll(it.data.filterNot { it.status=="新建" })
            }
            checkApproveAdapter.notifyDataSetChanged()
        }
        merchantViewModel.mErrorLiveData.observe(this){
            ToastUtils.showLong(it)
        }
        checkApproveAdapter.setOnItemClickListener { adapter, view, position ->
            ARouter.getInstance().build("/enterprise/MerchantXiangActivity")
                .withInt("merchanId",checkApproveAdapter.data[position].id)
                .navigation()
        }
    }
}